      *> File Name     000016_Exceptions.cbl.
      *> Description   Handling exceptions with the TRY statement.
      *> Project Type  Console Application.
      *> CodePlex URL  http://cobol.codeplex.com
 
       program-id.   Main.
       repository.
           class sys-console   as "System.Console"
           class sys-exception as "System.Exception"
           class fmt-exception as "System.FormatException"
           class io-exception  as "System.IO.IOException"
           class io-writer     as "System.IO.StreamWriter"
           .           
       working-storage section.
       01  sys-ex  sys-exception.
       01  fmt-ex  fmt-exception.
       01  io-ex   io-exception.
       01  writer  io-writer.
       01  s       string.
       procedure division.
           move "Handling exceptions with the TRY statement"
               to sys-console::"Title"
               
           move io-writer::"New"("test.txt") to writer
           
           try
               move string::"Format"("{{0},{1}}", 0, 1) to s *> ouch!
               invoke writer::"WriteLine"(s)
               display "Output succeeded."
           catch fmt-ex
               call "Gripe" using fmt-ex as sys-exception
           catch io-ex
               call "Gripe" using io-ex as sys-exception
           catch sys-ex
               call "Gripe" using sys-ex
           finally
               display "The FINALLY block executes regardless."
               invoke writer::"Close"
           end-try
               
           display "Press any key to continue . . . " no advancing
           invoke sys-console::"ReadKey"(true)
           .
       end program   Main.

       function-id.  Gripe.
       procedure division using ex as type "System.Exception".
           display "An exception of type " ex::"GetType" " occurred."
           display ex::"Message"
           .
       end function  Gripe.